package com.foo.SpatialDataProvider;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.Query;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.foo.DAO.SpatialDAO;
import com.foo.DataProvider.ProviderInterface;
import com.foo.DataSender.DataSender;
import com.foo.Entity.TickerInfoData;


/**
 * Session Bean implementation class SpatialDataProvider
 */
@Stateless(mappedName = "SpatialDataProvider/remote")
public class SpatialDataProvider implements ProviderInterface{ // SpatialDataProviderRemote, SpatialDataProviderLocal {

	int total =0 ;
	String spatialmessage = ""; 
	DataSender datasender = new DataSender();
	
    public SpatialDataProvider() 
    {
    	System.out.println("Spatial Data Provider created!");
    }

    @Override
    public void getData(String sorttype, String value, Timestamp timestamp, Date fromdate, Date todate)
	{
		System.out.println("Entering server function Spatial Data with parameters " +sorttype + " " +value);

			System.out.println("SPATIAL PROVIDER: GET DATA:");
			// Example for using named query
			spatialmessage = ""; 
    		ApplicationContext context = new ClassPathXmlApplicationContext("/beans.xml");
    		SpatialDAO forumDAO = (SpatialDAO) context.getBean("spatialDAO");   		
    		TickerInfoData[] datalist=forumDAO.selectForum(sorttype,value);		
    	
    		if (datalist !=null)
    		{
    		for(int k=0 ; k< datalist.length ; k++)
    		{
    			spatialmessage += datalist[k].getId()+","+datalist[k].getTicker_symbol()+","+ datalist[k].getCompany() +"," +datalist[k].getSector_name() 
    			+","+datalist[k].getIndustry()+ ","+datalist[k].getCountry()+","+datalist[k].getState()+","+datalist[k].getMarket_cap()+",";

    		}
    		}
    		else
    		{
    			System.out.println("The message is empty" );
    		spatialmessage ="";	
    		}
    		
		System.out.println("The message " +spatialmessage);
		datasender.sendMessage("Spatial",spatialmessage);
	}
}
 